package com.itstyle.quartz.web;

import com.itstyle.quartz.entity.Result;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * 通用访问拦截匹配
 * 创建者 灵伴即时
 * 创建时间	2018年4月3日
 */
@Controller
public class IndexController {

    @Value("${login.userName}")
    private String duserName;

    @Value("${login.passwd}")
    private String dpasswd;


    /**
     * 页面跳转
     *
     * @param url
     * @return
     */
    @RequestMapping("{url}.shtml")
    public String page(@PathVariable("url") String url) {
        System.out.println(">>>>>>>>>>>> url: " + url);
        return url;
    }

    /**
     * 页面跳转(二级目录)
     *
     * @param module
     * @param url
     * @return
     */
    @RequestMapping("{module}/{url}.shtml")
    public String page(@PathVariable("module") String module, @PathVariable("url") String url) {
        return module + "/" + url;
    }

    /**
     * 登录
     * @param userName
     * @param passwd
     * @param request
     * @return
     */
    @PostMapping("/login")
    @ResponseBody
    public Result login(String userName, String passwd, HttpServletRequest request) {
        if (duserName.equals(userName) && dpasswd.equals(passwd)) {
            request.getSession().setAttribute("loginName", userName);
            return Result.ok();
        }
        return Result.error("帐号或密码错误");
    }

    /**
     * 注销登录
     *
     * @param request
     * @return
     */
    @RequestMapping("/loginout")
    public String loginOut(HttpServletRequest request) {
        request.getSession().invalidate();
        return "redirect:/";
    }
}
